package com.zyh.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.netflix.discovery.converters.Auto;
import com.sun.org.apache.regexp.internal.RE;
import com.zyh.entity.Brand;
import com.zyh.entity.BrandCategoryDto;
import com.zyh.entity.CategoryBrandRelation;
import com.zyh.entity.ResultEntity;
import com.zyh.service.IBrandService;
import com.zyh.service.ICategoryBrandRelationService;
import com.zyh.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhang
 * @since 2022-05-11
 */
@RestController
@RequestMapping("/brand")
public class BrandController {
    @Value("${imgPath}")//获取springboot配置文件中的变量,把配置文件中的变量赋值给该属性
    private String path;
    @Autowired
    private IBrandService brandService;
    @Autowired
    private ICategoryService categoryService;
    @Autowired
    private ICategoryBrandRelationService categoryBrandRelationService;

    //根据类别的编号查询该类别下面的品牌信息，分页查询
    @GetMapping("/selectBrandByCategoryId/{categoryId}")
    public ResultEntity<IPage<Brand>> selectBrandByCategoryId(@PathVariable("categoryId") Integer categoryId,@RequestBody Page page){
        IPage<Brand> brandIPage = null;
        int category_id = categoryBrandRelationService.count(new QueryWrapper<CategoryBrandRelation>().eq("category_id", categoryId));
        if(category_id>0){
            List<Object> objects = categoryBrandRelationService.listObjs(new QueryWrapper<CategoryBrandRelation>()
                    .eq("category_id", categoryId)
                    .select("brand_id"));
            brandIPage = brandService.page(page, new QueryWrapper<Brand>().in("brand_id", objects));
        }
        return ResultEntity.ok(brandIPage);
    }

    //添加品牌
    @PostMapping("/add")
    public ResultEntity add(@RequestBody Brand brand){
        return brandService.save(brand)==true ? ResultEntity.ok() : ResultEntity.error();
    }

    //品牌图片上传
    @PostMapping("/uploadImg")
    public ResultEntity uploadImg(MultipartFile file) throws Exception{
        if(file==null){
            return ResultEntity.error("空的文件！");
        }
        file.transferTo(new File(path,file.getOriginalFilename()));
        return ResultEntity.ok();
    }

    //删除品牌
    @DeleteMapping("/delete/{brandId}")
    public ResultEntity delete(@PathVariable("brandId") Integer brandId){
        return brandService.removeById(brandId)==true ? ResultEntity.ok() : ResultEntity.error("删除失败！");
    }

    //根据品牌ID查询品牌信息
    @GetMapping("/selectOne/{brandId}")
    public ResultEntity<Brand> selectOne(@PathVariable("brandId") Integer brandId){
        return ResultEntity.ok(brandService.getById(brandId));
    }

    //修改品牌
    @PutMapping("/update")
    public ResultEntity update(@RequestBody Brand brand){
        return brandService.updateById(brand)==true ? ResultEntity.ok() : ResultEntity.error("修改失败！");
    }

    //给品牌关联商品类别(并去重)
    @PostMapping("/relaCate")
    public ResultEntity<List> relaCate(@RequestBody BrandCategoryDto brandCategoryDto){
        Integer brandId = brandCategoryDto.getBrandId();
        Integer[] categoryIds = brandCategoryDto.getCategoryIds();
        List checkFalse = new ArrayList();
        for (int i = 0; i < categoryIds.length; i++) {
            CategoryBrandRelation categoryBrandRelation = new CategoryBrandRelation();
            categoryBrandRelation.setBrandId(brandId.longValue());
            categoryBrandRelation.setBrandName(brandService.getById(brandId).getName());
            categoryBrandRelation.setCategoryId(categoryIds[i].longValue());
            categoryBrandRelation.setCategoryName(categoryService.getById(categoryIds[i]).getCategoryName());
            UpdateWrapper<CategoryBrandRelation> wrapper = new UpdateWrapper<CategoryBrandRelation>();
            wrapper.eq(true,"brand_id",brandId)
                    .eq(true,"category_id",categoryIds[i]);
            boolean save = categoryBrandRelationService.saveOrUpdate(categoryBrandRelation,wrapper);
            if(save){
                checkFalse.add(categoryBrandRelation);
            }
        }
        return ResultEntity.ok(checkFalse);
    }
}

